import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['ps.useafm'] = True
matplotlib.rcParams['pdf.use14corefonts'] = True


font = {'family' : 'serif',
        'color'  : 'black',
        'weight' : 'normal',
        'size'   : 24,
        }

constants = [
{"color":"purple","maker":"*","name":"LDA","linestyle":"-"},
{"color":"green","maker":"s","name":"BTM","linestyle":"-"},
{"color":"brown","maker":"D","name":"MU","linestyle":"-"},
{"color":"blue","maker":"^","name":"WNTM","linestyle":"-"},
{"color":"red","maker":"o","name":"RWNTM","linestyle":"-"}]
# #
# # constants = [{"color":"blue","maker":"^","name":"WNTM"},
# # {"color":"red","maker":"o","name":"WFWNTM"}]
#
# # d1,=ax.plot(x1, y1, color='blue', linestyle='dashed', marker='o', markerfacecolor='blue', markersize=6)
# # d2,=ax.plot(x1, y2, color='red', linestyle='dashed', marker='^', markerfacecolor='red', markersize=6)
# # d3,=ax.plot(x1, y3, color='black', linestyle='dashed', marker='s', markerfacecolor='black', markersize=6)
# # d4,=ax.plot(x1, y4, color='green', linestyle='dashed', marker='p', markerfacecolor='green', markersize=6)
# # d5,=ax.plot(x1, y5, color='yellow', linestyle='dashed', marker='*', markerfacecolor='yellow', markersize=6)
# # plt.axis([0.001,2,0.35,0.8])
# # plt.xscale('log')
# # plt.legend((d1,d2,d3,d4,d5),("PAM4ST","SP_SATM","LDA","unigram","DSTM"), loc=4)
# #
# # plt.xlabel('percentage of data removed',fontdict=font)
# # plt.ylabel('Precision',fontdict=font)
# # plt.title('')
# # plt.rc('font', size=18)
# # plt.show()
#
#
# def autolabel(rects):
#     for rect in rects:
#         height = rect.get_height()
#         plt.text(rect.get_x()+rect.get_width()/2., 1.03*height, '%s' % float(height))
#
#
def draw(host, x, y, x_titlet, y_title, type):
    bar_width = 2.5
    opacity = 0.7
    v_t = (x[1] - x[0])/4+2
    font = {'family': 'Arial',
            'color': 'black',
            'weight': 'normal',
            'size': 20,
            }

    min_y = min([min(item) for item in y])
    max_y =  max([max(item) for item in y])
    v_y = (max_y - min_y) /20

    host.set_xlim(x[0] - v_t, x[-1] + v_t)
    host.set_ylim(min_y - 2*v_y,max_y + 6*v_y)

    if x_titlet is not None:
        host.set_xlabel(x_titlet,fontdict=font)
    host.set_ylabel(y_title,fontdict=font)
    pts = []

    location_index = 1

    for kindex,yi in enumerate(y):
        index = 5 - len(y) + kindex
        if type == 'plot':
            pts.append(host.plot(x, yi, alpha=opacity, label=constants[index]["name"],lw=3, linestyle=constants[index]["linestyle"], marker=constants[index]["maker"], markersize=8, color=constants[index]["color"])[0])
        else:
            pts.append(host.bar([item + (kindex-2.5)*bar_width for item in x], yi, bar_width,alpha=opacity, color =constants[index]["color"],label=constants[index]["name"]))
            # autolabel(pts[-1])
    # p1, = host.plot(x, y1, label="Accuracy", linestyle='-', marker='o', markersize=3, color='red')
    # host.legend(loc='upper right')

    host.legend(pts, [item["name"] for item in constants[5 - len(y):5]], loc=location_index, ncol=3, fancybox=True)
#
#
x=[20,40,60,80,100]

# #coherence news 5
# y = [[-52.766, -56.655 ,-53.054 ,-57.065 ,-60.817 ],
# [-50.804 , -47.594 ,-48.694 ,-49.094 ,-47.840 ],
# [-50.625 , -60.339 ,-62.744 ,-63.783 ,-70.735 ],
# [-45.861 , -44.557 ,-48.551 ,-50.008 ,-49.598 ],
# [-42.215 , -45.413 ,-45.749 ,-46.597 ,-46.780 ]]

# #coherence news 10
# y = [[-265.234 ,-300.501 ,-311.079 ,-344.940 ,-375.482 ],
# [-259.731 ,-252.037 ,-269.874 ,-278.421 ,-281.985 ],
# [-310.947 ,-374.752 ,-404.072 ,-415.259 ,-448.809 ],
# [-252.716 ,-265.492 ,-276.753 ,-292.938 ,-295.105 ],
# [-231.312 ,-251.646 ,-258.032 ,-265.900 ,-274.929 ]]


# #coherence new-tweet 5
# y = [[-76.822 ,-83.745 ,-81.698 ,-84.472 ,-91.077 ],
# [-69.669 ,-80.278 ,-79.297 ,-85.620 ,-88.587 ],
# [-79.201 ,-95.395 ,-101.288 ,-107.609 ,-115.444 ],
# [-62.166 ,-66.579 ,-75.343 ,-69.875 ,-71.121 ],
# [-55.651 ,-58.033 ,-60.604 ,-58.826 ,-56.844 ]]

# #coherence new-tweet 10
# y = [[-412.644, -458.492, -475.233, -493.843, -537.767 ],
# [-397.063, -451.853, -487.727, -512.997, -527.187 ],
# [-486.060, -575.078, -621.036, -641.146, -701.834 ],
# [-343.366, -370.794, -404.077, -414.597, -422.531 ],
# [-292.040, -313.293, -318.057, -322.117, -336.259 ]]
#
# y_title = "F-measure"
# y_title = "Coherence"
#
# fig, axes = plt.subplots()
#
# # draw(axes[0], x, y1, None,y_title,  type = "plot")
# draw(axes, x, y, "Topic numbers", y_title,  type = "plot")
#
# fig.subplots_adjust(left=0.13, right=0.95, bottom=0.12,top = 0.95)
# plt.rc('font', size=16, family='Arial')
# plt.draw()
# plt.show()

#
# # coherence 20newsgroup 5 10
# y = [[-23.411 , -26.155 ,-26.111 ,-26.450 ,-27.180 ],
# [-22.626 , -25.062 ,-25.145 ,-25.959 ,-26.645 ]]
# # y = [[-114.740 , -125.026 ,-129.454 ,-132.767 ,-135.261 ],
# # [-111.687 , -122.784 ,-124.434 ,-127.370 ,-128.218 ]]
#
# x_title = "Topic numbers"
# y_title = "Coherence"
# fig, axes = plt.subplots()
# draw(axes, x, y, x_title , y_title,  type = "plot")
# # draw(axes, x, y, "Topic numbers", y_title,  type = "plot")
#
# fig.subplots_adjust(left=0.13, right=0.95, bottom=0.12,top = 0.95)
# plt.rc('font', size=16, family='Arial')
# # plt.draw()
# plt.show()


# #classification 20newsgroup
# y = [[0.540 ,0.653 ,0.676 ,0.698 ,0.706 ],
# [0.541 ,0.646 ,0.688 ,0.700 ,0.714 ]]
#
# y_title = "F-measure"
#
# fig, axes = plt.subplots()
#
# # draw(axes[0], x, y1, None,y_title,  type = "plot")
# draw(axes, x, y, "Topic numbers", y_title,  type = "bar")
#
# fig.subplots_adjust(left=0.13, right=0.95, bottom=0.12,top = 0.95)
# plt.rc('font', size=16, family='Arial')
# plt.draw()
# plt.show()


# #classification news
# y = [[0.745,0.756,0.763,0.764,0.767],
# [0.761 ,0.770 ,0.779 ,0.780 ,0.786],
# [0.716,0.750,0.756,0.764,0.773],
# [0.768 ,0.781 ,0.786 ,0.790 ,0.793],
# [0.768 ,0.782 ,0.786 ,0.794 ,0.798]]

#classification new-tweet
y = [[0.539,0.573,0.582,0.585,0.593],
[0.544 ,0.629 ,0.641 ,0.651 ,0.655 ],
[0.552,0.590,0.609,0.620,0.624],
[0.601 ,0.638 ,0.655 ,0.666 ,0.675 ],
[0.609 ,0.657 ,0.669 ,0.682 ,0.686 ]]

y_title = "F-measure"

fig, axes = plt.subplots()

# draw(axes[0], x, y1, None,y_title,  type = "plot")
draw(axes, x, y, "Topic numbers",y_title,  type = "bar")

fig.subplots_adjust(left=0.13, right=0.95, bottom=0.12,top = 0.95)
plt.rc('font', size=16, family='Arial')
plt.draw()
plt.show()